package ext.generic.doc.processors;

import com.ptc.netmarkets.util.beans.NmCommandBean;

import wt.doc.WTDocument;

import wt.fc.Persistable;
import wt.fc.collections.WTArrayList;

import com.ptc.core.components.util.FeedbackMessage;
import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.core.components.forms.*;

import java.util.*;

import wt.util.WTException;

import wt.workflow.work.WorkItem;
import wt.workflow.work.WfAssignedActivity;
import wt.workflow.engine.ProcessData;
import wt.workflow.engine.WfVariable;

import wt.session.SessionHelper;

import wt.util.WTMessage;
import wt.util.LocalizableMessage;

import com.ptc.netmarkets.util.misc.NmContext;

import wt.method.RemoteAccess;

import java.io.Serializable;

import com.ptc.netmarkets.model.NmOid;

import wt.identity.DisplayIdentity;
import wt.identity.IdentityFactory;

/**
 *文档流程中"移除随签对象"的action响应方法类
 */
public class RemoveReviewDocFormProcessor extends DefaultObjectFormProcessor implements RemoteAccess, Serializable
{

    public RemoveReviewDocFormProcessor()
    {
    }

    /**
     *文档流程中"移除随签对象"的具体方法
     * @param nmcommandbean
     * @param list
     * @return
     * @throws WTException
     */
    public FormResult postProcess(NmCommandBean nmcommandbean, List list) throws WTException
    {

        WTArrayList batchReviewDocs = new WTArrayList();
        NmOid nmoid = nmcommandbean.getPrimaryOid();
        WorkItem workitem = (WorkItem) nmoid.getRef();

        WfAssignedActivity wfassignedactivity = (WfAssignedActivity) workitem.getSource().getObject();
        ProcessData context = wfassignedactivity.getContext();
//        WfVariable[]array = context.getVariableList();
//        System.out.print("array.length="+array.length);
//        for(int i=0;i<array.length;i++)
//        {
//        	System.out.println("array["+i+"]="+array[i].getName()+array[i].getValue());
//        }
        batchReviewDocs = (WTArrayList) (context.getValue("reviewObjs"));
        WTDocument pbo = (WTDocument) batchReviewDocs.getPersistable(0);
    //    WTDocument pbo = (WTDocument) batchReviewDocs.get(0);
        java.util.Locale locale = SessionHelper.getLocale();
        ArrayList arraylist = nmcommandbean.getSelected();

        if (arraylist.size() == 0)
        {
            FormResult formresult = new FormResult();
            FeedbackMessage feedbackmessage =
                new FeedbackMessage(FeedbackType.FAILURE, locale, new WTMessage("ext.generic.doc.docResource", "NO_REVIEWDOC_SELECTED", null), null, new LocalizableMessage[]
                    { new WTMessage("ext.generic.doc.docResource", "NO_REVIEWDOC_SELECTED", null) });
            formresult.addFeedbackMessage(feedbackmessage);
            formresult.setStatus(FormProcessingStatus.FAILURE);
            formresult.setNextAction(FormResultAction.REFRESH_CURRENT_PAGE);
            return formresult;
        }
        else
        {
            for (int i = 0; i < arraylist.size(); i++)
            {
                NmContext nmcontext = (NmContext) arraylist.get(i);
                Persistable persistable = nmcontext.getTargetOid().getWtRef().getObject();
                if (!(persistable instanceof WTDocument))
                    continue;
                WTDocument wtdocument = (WTDocument) nmcontext.getTargetOid().getWtRef().getObject();
                System.out.println("******************************* process Object="+pbo);
                System.out.println("********************************wtdocument="+wtdocument);
                
                if (wtdocument.equals(pbo))
                {
                    String s = WTMessage.getLocalizedMessage("ext.generic.doc.docResource", "REMOVED_REVIEW_DOC_FAILED", null);
                    String s1 = WTMessage.getLocalizedMessage("ext.generic.doc.docResource", "PBO_CAN_NOT_REMOVED", null);
                    StringBuffer stringbuffer1 = new StringBuffer(s1);
                    FormResult formresult1 = new FormResult();
                    Object obj = (Object) wtdocument;
                    DisplayIdentity displayidentity1 = IdentityFactory.getDisplayIdentity(wtdocument);
                    stringbuffer1.append("\n   ");
                    stringbuffer1.append(displayidentity1.getLocalizedMessage(locale));

                    FeedbackMessage feedbackmessage1 = new FeedbackMessage(FeedbackType.FAILURE, locale, s, null, new String[]
                            { stringbuffer1.toString() });

                    formresult1.addFeedbackMessage(feedbackmessage1);                   
                    formresult1.setStatus(FormProcessingStatus.FAILURE);
                    formresult1.setNextAction(FormResultAction.REFRESH_CURRENT_PAGE);
                   return formresult1;
                }
                batchReviewDocs.remove(wtdocument);           
            }
        }
        context.setValue("reviewObjs", batchReviewDocs);
        wfassignedactivity.setContext(context);
        wt.fc.PersistenceHelper.manager.save(wfassignedactivity);
        FormResult formresult1 = new FormResult();
        formresult1.setStatus(FormProcessingStatus.SUCCESS);
        System.out.println("ppppppppppppppppppppppppppppppppppppp");
        formresult1.setNextAction(FormResultAction.REFRESH_CURRENT_PAGE);
        System.out.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpqqqqqq");
        super.postProcess(nmcommandbean, list);
        formresult1.setNextAction(FormResultAction.REFRESH_CURRENT_PAGE);
        return formresult1;
    }

    protected FormResult setRefreshInfo(FormResult formresult, NmCommandBean nmcommandbean, List list) throws WTException
    {
        DynamicRefreshInfo dynamicrefreshinfo = new DynamicRefreshInfo();
        dynamicrefreshinfo.setLocation(nmcommandbean.getActionOid());
        dynamicrefreshinfo.setOid(nmcommandbean.getActionOid());
        NmCommandBean _tmp = nmcommandbean;
        dynamicrefreshinfo.setAction("U");
        formresult.addDynamicRefreshInfo(dynamicrefreshinfo);
        return formresult;
    }
}
